/********************************
 * ETK - Easy ToolKit
 * Copyright (C) 2007-2008 Ekinoks (scv.ekinoks@gmail.com)
 * All rights reserved.
 *
 *    This file is part of ETK.
 *
 *    Foobar is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    Foobar is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Foobar; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ***********************************************************************************/

#ifndef __ETK_CALLBACKT_H__
#define __ETK_CALLBACKT_H__

#include <boost/function.hpp>
#include <boost/bind.hpp>


//#include "../../define.h"
//#include "../../Event/Event.h"
#include "../CallBackI.h"
#include "../../../tool/toFunction.h"
#include "../../LIST_EVENT.H"

namespace etk
{

    class Widget;
    class Event;


    /** Pour créer des CallBacks */
    template <class EventType=Event>
    class CallBackT : public CallBackI
    {
        //event_e event;

        boost::function1<void, EventType> f;

        boost::function1<bool, EventType> filter;

        EventType e;

        bool bfilter;

        public :

            /**
            *   @param widget   : Widget sur le quel le CallBack est créé
            *   @param event    : Evenement déclancheur
            *   @param f        : Fonction a appeler en cas de déclanchement
            *   @param after    :
            */
            template<class F>
            CallBackT(Widget *widget_, event_e event_, F f_, bool after=false);

            CallBackT* setFilter(boost::function1<bool, EventType> filter)
            {
                bfilter = true;
                this->filter = filter;
                return this;
            }

            void call(void *event=NULL)
            {
                if(event!=NULL)
                    e.updateEvent(event);

                if(!bfilter || filter(e))
                {
                    f(e);
                }
            }

            ~CallBackT()
            {

            }

        private :
            template <class D>
            boost::function1<void, EventType> bindWidget( boost::function2<void, D*, EventType, std::allocator<boost::function_base> > f, Widget *widget )
            {
                return boost::bind(f, dynamic_cast<D*>(widget), _1);
            }
    };

}

#include "CallBackT.tpp"

#endif /* #ifndef __ETK_CALLBACKT_H__ */

